package com.wswy.wzcx.ui.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Space;
import android.widget.TextView;

import com.che.libcommon.ui.BaseFragment;
import com.wswy.wzcx.R;
import com.wswy.wzcx.model.DataCenter;
import com.wswy.wzcx.model.home.RouterUtils;
import com.wswy.wzcx.ui.other.AndroidUtilities;
import com.wswy.wzcx.ui.other.DialogFactory;
import com.wswy.wzcx.ui.other.LayoutHelper;

/**
 * Created by wangduheng26 on 2018/1/22.
 * wangduheng26@gmail.com
 */

public abstract class PwdFragment extends BaseFragment {
    protected TextView tvSubmit;
    protected EditText editText;
    protected CheckBox imageView;
    protected TextView textView;
    protected TextView tvTitle;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Context context = container.getContext();
        LinearLayout rootLayout = new LinearLayout(context);
        rootLayout.setOrientation(LinearLayout.VERTICAL);
        //rootLayout.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.default_background_color));
        Space space = new Space(context);
        rootLayout.addView(space, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 20));


        LinearLayout outLayer = new LinearLayout(context);
        outLayer.setBackgroundColor(Color.WHITE);
        outLayer.setOrientation(LinearLayout.VERTICAL);
        outLayer.setPadding(AndroidUtilities.dp(15), AndroidUtilities.dp(10), AndroidUtilities.dp(15), AndroidUtilities.dp(6));

        tvTitle = new TextView(context);
        tvTitle.setMinHeight(AndroidUtilities.dp(24));
        tvTitle.setText("手机号码");
        tvTitle.setGravity(Gravity.BOTTOM);
        outLayer.addView(tvTitle);


        LinearLayout layoutInput = new LinearLayout(context);
        layoutInput.setOrientation(LinearLayout.HORIZONTAL);

        editText = new EditText(context);
        editText.setBackgroundColor(Color.TRANSPARENT);
        editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        editText.setHint("请输入密码");
        editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
        editText.setTextColor(ContextCompat.getColor(context, R.color.gray20));
        editText.setHintTextColor(ContextCompat.getColor(context, R.color.hint_color));
        editText.setMaxEms(20);
        layoutInput.addView(editText, LayoutHelper.createLinear(0, LayoutHelper.WRAP_CONTENT, 1f));

        imageView = new CheckBox(context);
        imageView.setBackgroundColor(Color.TRANSPARENT);
        imageView.setButtonDrawable(0);
        imageView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.input_pwd_select, 0, 0, 0);
        layoutInput.addView(imageView, LayoutHelper.createLinear(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT));

        outLayer.addView(layoutInput, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT));

        rootLayout.addView(outLayer, LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT));


        textView = new TextView(context);
        textView.setText("请设置6-20位密码，不能有空格");
        textView.setPadding(AndroidUtilities.dp(15), AndroidUtilities.dp(12), 0, 0);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
        textView.setTextColor(ContextCompat.getColor(context, R.color.gray60));
        rootLayout.addView(textView);

        tvSubmit = new TextView(context);
        tvSubmit.setTextColor(Color.WHITE);
        tvSubmit.setGravity(Gravity.CENTER);
        tvSubmit.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
        tvSubmit.setBackgroundResource(R.drawable.btn_submit);
        LayoutHelper.setSelectable(tvSubmit);
        tvSubmit.setEnabled(false);
        tvSubmit.setText(R.string.ok);
        LinearLayout.LayoutParams linear = LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, 44);
        rootLayout.addView(tvSubmit, LayoutHelper.setMargin(linear, 20,30,20,0));
        return rootLayout;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        tvSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //todo
                submit();
            }
        });
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s) || s.length() < 6) {
                    tvSubmit.setEnabled(false);
                } else {
                    tvSubmit.setEnabled(true);
                }
            }
        });
        imageView.setChecked(false);
        imageView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                editText.setInputType(isChecked ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD : InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                final Editable text = editText.getText();
                if (!TextUtils.isEmpty(text)) editText.setSelection(text.length());
            }
        });
    }

    protected abstract void submit();

    protected void showSuccessDialog() {
        AlertDialog.Builder builder = DialogFactory.create(getContext(), "设定成功", "密码设定成功，以后可以使用帐号密码登录");
        builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                finish();
            }
        });
        builder.show();
    }

    protected void showReLoginDialog() {
        DataCenter.get().cleanUser();
        AlertDialog.Builder builder = DialogFactory.create(getContext(), "修改成功", "密码修改成功，请重新登录");
        builder.setPositiveButton(R.string.go_login, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startActivity(RouterUtils.getLogin());
                finish();
            }
        });
        builder.show();
    }
}
